package com.ph.irp.base.salespricepolicy.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.salesprice.service.PBaseSalespriceService;
import com.ph.irp.base.salespricepolicy.service.SalesPricePolicyService;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.SalespriceDTO;
import com.ph.irp.model.PBaseSalesprice;
import com.ph.irp.model.PBaseSalespricepolicy;
import com.ph.util.pagination.PageUtil;

public class SalesPricePolicyAction extends ActionSupport {
	@SuppressWarnings("unused")
	private static String FORWORD = null;
	private List<PBaseSalespricepolicy> salespricepolicyList = null;
	private PBaseSalespricepolicy salespricepolicyModel = null;
	private SalesPricePolicyService salespricepolicyService;
	private PBaseSalespriceService salespriceService;
	private String successString;
	private String FORWARD = null;
	private Object[] rowlist;
	private List<Integer> list = new ArrayList<Integer>();
	
	private List<SalespriceDTO> tempSalespriceDTOList;
	private List<SalespriceDTO> salespriceDTOList;
	private PBaseSalesprice salesprice;
	private SalespriceDTO salespriceDTO;
	private int customerStytle;
	private Object[] salelist;

	private PageDTO page;
	private String cpage;
	

	public List<PBaseSalespricepolicy> getSalespricepolicyList() {
		return salespricepolicyList;
	}

	public void setSalespricepolicyList(
			List<PBaseSalespricepolicy> salespricepolicyList) {
		this.salespricepolicyList = salespricepolicyList;
	}

	public PBaseSalespricepolicy getSalespricepolicyModel() {
		return salespricepolicyModel;
	}

	public void setSalespricepolicyModel(
			PBaseSalespricepolicy salespricepolicyModel) {
		this.salespricepolicyModel = salespricepolicyModel;
	}

	public SalesPricePolicyService getSalespricepolicyService() {
		return salespricepolicyService;
	}

	public void setSalespricepolicyService(
			SalesPricePolicyService salespricepolicyService) {
		this.salespricepolicyService = salespricepolicyService;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public List<Integer> getList() {
		return list;
	}

	public void setList(List<Integer> list) {
		this.list = list;
	}

	/*
	 * 查询所有数据
	 */
	public String showSalespricepolicyList() throws Throwable {
		salespricepolicyModel = null;
		salespricepolicyList = salespricepolicyService
				.getSalesPricePolicyList(salespricepolicyModel);
		if (salespricepolicyList != null)
			FORWARD = "success";
		else
			FORWARD = "input";
		return FORWARD;
	}

	/*
	 * 打开新增页面
	 */
	public String showAddSalespricepolicy() {
		salespricepolicyModel = null;
		FORWARD = "success";
		return FORWARD;
	}

	/*
	 * 打开修改页面
	 */
	public String showEditSalespricepolicy() throws Throwable {
		salespricepolicyModel = salespricepolicyService
				.getSalesPricePolicyById(Integer.valueOf((String) rowlist[0]));
		if (salespricepolicyModel != null)
			FORWARD = "success";
		else
			FORWARD = "input";
		return FORWARD;
	}

	/*
	 * 新增
	 */
	public String addSalespricepolicy() throws Throwable {
		salespricepolicyService.addSalesPricePolicy(salespricepolicyModel);
		salespricepolicyModel = null;
		FORWARD = "success";
		return FORWARD;
	}

	/*
	 * 修改
	 */
	public String editSalespricepolicy() throws Throwable {
		salespricepolicyService.updateSalesPricePolicy(salespricepolicyModel);
		salespricepolicyModel = null;
		FORWARD = "success";
		return FORWARD;
	}

	/*
	 * 查询
	 */
	public String getSalespricepolicy() throws Throwable {
		FORWARD = "success";
		if (salespricepolicyModel.getSalesPricePolicyCode() != null
				&& salespricepolicyModel.getSalesPricePolicyName() != null) {
			salespricepolicyList = salespricepolicyService.getByNameAndCode(
					salespricepolicyModel.getSalesPricePolicyName(),
					salespricepolicyModel.getSalesPricePolicyCode());
		} else if (salespricepolicyModel.getSalesPricePolicyCode() != null) {
			salespricepolicyList = salespricepolicyService
					.getByCode(salespricepolicyModel.getSalesPricePolicyCode());
		} else if (salespricepolicyModel.getSalesPricePolicyName() != null) {
			salespricepolicyList = salespricepolicyService
					.getByName(salespricepolicyModel.getSalesPricePolicyName());
		} else {
			FORWARD = "input";
		}
		salespricepolicyModel = null;
		return FORWARD;
	}

	/*
	 * 删除币别
	 */
	public String deleteSalespricepolicy() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			salespricepolicyService.deleteSalesPricePolicy(Integer
					.parseInt((String) rowlist[i]));
		}
		FORWARD = "success";
		return FORWARD;
	}

	
	
	/*
	 * 查询所有数据
	 */
	public String showSalespriceList() throws Throwable {
		salespriceDTOList = new ArrayList<SalespriceDTO>();
		salesprice = new PBaseSalesprice();
		int i = Integer.parseInt((String) rowlist[0]);
		salesprice.setSalespricepolicyId(i);
		salesprice.setAuditState(2);
		tempSalespriceDTOList = salespriceService
				.getSalespriceByModel(salesprice);
		if (tempSalespriceDTOList != null) {
			page = PageUtil.generatePageDTO(tempSalespriceDTOList,
					"showSalespricePageList.action", cpage);
			salespriceDTOList = page.getResList();
			FORWORD = "success";
		} else {
			setSuccessString("没有此销售价格");
			FORWORD = "input";
		}
		return FORWORD;
	}

	public String showPageList() throws Throwable {
		if (tempSalespriceDTOList != null) {
			page = PageUtil.generatePageDTO(tempSalespriceDTOList,
					"showSalespricePageList.action", cpage);
			salespriceDTOList = page.getResList();
			FORWORD = "success";
		} else {
			setSuccessString("没有此销售价格");
			FORWORD = "input";
		}
		return FORWORD;
	}

	/*
	 * 查询
	 */
	public String getSalespriceBy() throws Throwable {
		int i = Integer.parseInt((String) rowlist[0]);
		salesprice.setSalespricepolicyId(i);
		tempSalespriceDTOList = salespriceService
				.getSalespriceByModel(salesprice);
		salesprice = null;
		if (tempSalespriceDTOList != null) {
			page = PageUtil.generatePageDTO(tempSalespriceDTOList,
					"showSalespricePageList.action", cpage);
			salespriceDTOList = page.getResList();
			FORWORD = "success";
		} else {
			FORWORD = "input";
		}
		salesprice = null;
		return FORWORD;
	}

	/*
	 * 打开新增页面
	 */
	public String showAddSalesprice() {
		salespriceDTO = null;
		salesprice = null;
		FORWORD = "success";
		return FORWORD;
	}

	/*
	 * 打开修改页面
	 */
	public String showEditSalesprice() throws Throwable {
		salesprice = new PBaseSalesprice();
		int salespriceId = Integer.parseInt((String) salelist[0]);
		salesprice = salespriceService.getSalespriceByID(salespriceId);
		if (salesprice.getAuditState() == 1) {
			setSuccessString("审核过的记录不能被修改，请先反审核");
			FORWORD = "input";
		} else {
			salesprice.setSalesPriceId(salespriceId);
			salespriceDTO = new SalespriceDTO();
			salespriceDTOList = salespriceService
					.getSalespriceByModel(salesprice);
			salespriceDTO = salespriceDTOList.get(0);
			salesprice = salespriceDTO.getSalesprice();
			FORWORD = "success";
		}
		return FORWORD;
	}

	/*
	 * 新增
	 */
	public String addSalesprice() throws Throwable {
		int i = Integer.parseInt((String) rowlist[0]);
		salesprice.setSalespricepolicyId(i);
		salespriceService.addSalesprice(salesprice);
		salesprice = null;
		FORWORD = "success";
		return FORWORD;
	}

	/*
	 * 修改
	 */
	public String editSalesprice() throws Throwable {
		int i = Integer.parseInt((String) rowlist[0]);
		salesprice.setSalespricepolicyId(i);
		salespriceService.updateSalesprice(salesprice);
		salesprice = null;
		FORWORD = "success";
		return FORWORD;
	}

	/*
	 * 删除币别
	 */
	public String deleteSalesprice() throws Throwable {
		FORWORD = "success";
		for (int i = 0; i < salelist.length; i++) {
			int salespriceId = Integer.parseInt((String) salelist[i]);
			salesprice = salespriceService.getSalespriceByID(salespriceId);
			if (salesprice.getAuditState() == 0) {
				salespriceService.delectSalesprice(salespriceId);
			} else {
				setSuccessString("审核过的记录不能被删除，请先反审核");
				FORWORD = "input";
			}
		}
		return FORWORD;
	}

	public String auditingSalesprice() throws Throwable {
		salesprice = new PBaseSalesprice();
		int salespriceId = Integer.parseInt((String) salelist[0]);
		salesprice = salespriceService.getSalespriceByID(salespriceId);
		if (salesprice.getAuditState() == 0) {
			salesprice.setAuditState(1);
			salespriceService.updateSalesprice(salesprice);
		}
		return "success";
	}

	public String reAuditingSalesprice() throws Throwable {
		salesprice = new PBaseSalesprice();
		int salespriceId = Integer.parseInt((String) salelist[0]);
		salesprice = salespriceService.getSalespriceByID(salespriceId);
		if (salesprice.getAuditState() == 1) {
			salesprice.setAuditState(0);
			salespriceService.updateSalesprice(salesprice);
		}
		return "success";
	}

	public String showControlSalesprice() throws Throwable {
		salesprice = new PBaseSalesprice();
		salespriceDTOList = new ArrayList<SalespriceDTO>();
		int salespriceId = Integer.parseInt((String) salelist[0]);
		salesprice.setSalesPriceId(salespriceId);
		salesprice.setAuditState(2);
		salespriceDTOList = salespriceService.getSalespriceByModel(salesprice);
		salespriceDTO = salespriceDTOList.get(0);
		FORWORD = "success";
		return FORWORD;
	}

	// //限价返回
	// public String reback() throws Throwable{
	// salesprice = new PBaseSalesprice();
	// salesprice.setSalespricepolicyId(salespriceDTO.getSalesprice().getSalespricepolicyId());
	// salesprice.setAuditState(2);
	// return getSalespriceBy();
	// }

	// 修改限价
	@SuppressWarnings("unchecked")
	public String controlSalesprice() throws Throwable {
		String message = null;
		if (customerStytle == 0) {
			salesprice = new PBaseSalesprice();
			salesprice = salespriceDTO.getSalesprice();
			salesprice.setBottomBid(salespriceDTO.getSalesprice()
					.getBottomBid());
			salespriceService.updateSalesprice(salesprice);
		} else {
			salesprice = new PBaseSalesprice();
			salesprice.setSalespricepolicyId(salespriceDTO.getSalesprice()
					.getSalespricepolicyId());
			salesprice.setAuditState(2);
			salesprice.setMaterialId(salespriceDTO.getSalesprice()
					.getMaterialId());
			salespriceDTOList = salespriceService
					.getSalespriceByModel(salesprice);
			Iterator it = salespriceDTOList.iterator();

			while (it.hasNext()) {
				PBaseSalesprice sp = ((SalespriceDTO) it.next())
						.getSalesprice();
				if (salespriceDTO.getSalesprice().getBottomBid() < sp.getBid()) {
					sp.setBottomBid(salespriceDTO.getSalesprice()
							.getBottomBid());
					salespriceService.updateSalesprice(sp);
				} else {
					message = "限价高于某客户的报价，请核实或单个限价";
				}

			}
		}
		if (message != null) {
			setSuccessString(message);
			FORWORD = "input";
		} else {
			FORWORD = "success";
		}
		return FORWORD;
	}
	
	
	
	
	
	// /*
	// * 查询方法
	// */
	// public String select() throws Throwable{
	// salespricepolicyList=salespricepolicyService.getSalesPricePolicyList(salespricepolicyModel);
	// salespricepolicyModel=null;
	// if(salespricepolicyList!=null){
	// FORWORD="success";
	// }else {
	// setSuccessString("没有此销售价格政策");
	// FORWORD="error";
	// }
	// return FORWORD;
	// }
	//	
	// /*
	// * 打开新增或修改页面
	// */
	// public String addInput(){
	// FORWORD = "addInput";
	// salespricepolicyModel=null;
	// return FORWORD;
	// }
	//	
	// /*
	// * 新增记录
	// */
	// public String add() throws Throwable{
	//		
	// if(salespricepolicyModel.getSalesPricePolicyId()!=0){
	// salespricepolicyService.updateSalesPricePolicy(salespricepolicyModel);
	// salespricepolicyModel=null;
	// }
	// else {
	// salespricepolicyService.addSalesPricePolicy(salespricepolicyModel);
	// salespricepolicyModel=null;
	// }
	// FORWORD="display";
	// return FORWORD;
	// }
	//	
	// /*
	// * 修改记录
	// */
	// public String update() throws Throwable{
	// salespricepolicyModel=salespricepolicyService.getSalesPricePolicyById(Integer.valueOf((String)
	// rowlist[0]));
	// FORWORD ="addInput";
	// return FORWORD;
	// }
	//	
	// /*
	// * 删除记录
	// */
	// public String delete() throws Throwable{
	// FORWORD="display";
	// // int result=0;
	// for(int i=0;i<rowlist.length;i++){
	// int salespricePolicyId = Integer.valueOf((String) rowlist[i]);
	// PBaseSalesprice salesprice = new PBaseSalesprice();
	// salesprice.setSalespricepolicyId(salespricePolicyId);
	// List<SalespriceDTO> salespriceDTOList =
	// salespriceService.getSalespriceByModel(salesprice);
	// if(salespriceDTOList.size()==0){
	// salespricepolicyService.deleteSalesPricePolicy(salespricePolicyId);
	// }else{
	// setSuccessString("该销售价格政策被引用，无法被删除");
	// return ERROR;
	// }
	//			
	// result=salespricepolicyService.deleteSalesPricePolicy(Integer.valueOf((String)
	// rowlist[i]));
	// }

	// if (result==1) {
	// FORWORD="display";
	// } else {
	// setAlertMessage("没有此销售价格政策");
	// FORWORD="error";
	// }
	// return FORWORD;
	// }

	// /*
	// * 打开销售价格资料页面
	// */
	// public String open(){
	// FORWORD ="open";
	// return FORWORD;
	// }
	public String getSuccessString() {
		return successString;
	}

	public void setSuccessString(String successString) {
		this.successString = successString;
	}

	public PBaseSalespriceService getSalespriceService() {
		return salespriceService;
	}

	public void setSalespriceService(PBaseSalespriceService salespriceService) {
		this.salespriceService = salespriceService;
	}

	public String getFORWARD() {
		return FORWARD;
	}

	public void setFORWARD(String forward) {
		FORWARD = forward;
	}

	public List<SalespriceDTO> getTempSalespriceDTOList() {
		return tempSalespriceDTOList;
	}

	public void setTempSalespriceDTOList(List<SalespriceDTO> tempSalespriceDTOList) {
		this.tempSalespriceDTOList = tempSalespriceDTOList;
	}

	public List<SalespriceDTO> getSalespriceDTOList() {
		return salespriceDTOList;
	}

	public void setSalespriceDTOList(List<SalespriceDTO> salespriceDTOList) {
		this.salespriceDTOList = salespriceDTOList;
	}

	public PBaseSalesprice getSalesprice() {
		return salesprice;
	}

	public void setSalesprice(PBaseSalesprice salesprice) {
		this.salesprice = salesprice;
	}

	public SalespriceDTO getSalespriceDTO() {
		return salespriceDTO;
	}

	public void setSalespriceDTO(SalespriceDTO salespriceDTO) {
		this.salespriceDTO = salespriceDTO;
	}

	public int getCustomerStytle() {
		return customerStytle;
	}

	public void setCustomerStytle(int customerStytle) {
		this.customerStytle = customerStytle;
	}

	public Object[] getSalelist() {
		return salelist;
	}

	public void setSalelist(Object[] salelist) {
		this.salelist = salelist;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

}
